Useful Python by Stuart Langridge
Author:Stuart Langridge [Stuart Langridge]
Language: eng
Format: epub
ISBN: 9781925836578
Publisher: SitePoint Pty. Ltd.
Published: 2022-04-06T23:00:00+00:00
For this, weâll need to read the metadata from Python, and the best way to do any image manipulation is with Pillow, the Python Imaging Library. The metadata we want here is called IPTC, defined in extreme detail by the International Press Telecommunications Council in the IPTC Photo Metadata Standard. In particular, we want the imageâs title tag, with code 2:120. We can define a convenient variable for that:
IPTC_Title = (2, 120)
Next, letâs loop over each of our images. Pythonâs glob module is useful for this. A glob (a term from Unix prehistory, short for âglobalâ) is a pattern using a wildcard character and file specification to gather a list of all files matching that pattern. (For example, *.txt will match a list of files such as myfile.txt, 1.txt, everything.txt, and so on.) In Python, glob.glob("*.txt") does the same thing. It returns a list of matching filenames, which we can then iterate over. We can also open each one with the Python Imaging Library:
import glob, os from PIL import Image for image_filename in glob.glob("*.jpg"): im = Image.open(image_filename)
Here, we have a reference to each image. Pillow can do all sorts of image manipulations, such as resizing, changing the content, saving in different formats, and so on. But for now, all we want to do is read the metadata. For that, we use Pillowâs IptcImagePlugin, and we can have it print out the relevant image title:
from PIL import Image, IptcImagePlugin for image_filename in glob.glob("*.jpg"): im = Image.open(image_filename) iptc = IptcImagePlugin.getiptcinfo(im) im.close() title = iptc[IPTC_Title].decode("utf-8")
Now that we know the title for a given image, we can use it to construct a new filename, and then use os.rename to rename the file to this new name. The os.rename function does more than simply renaming; it can also move a file to a different directory or different disk. But in this case, weâre only renaming the file in place:
destination_filename = f"{title}.jpg" print(f"Renaming {image_filename} -> {destination_filename}") if os.path.exists(destination_filename): print(f"{destination_filename} already exists; skipping") else: os.rename(image_filename, destination_filename)
Weâre being a little careful here. If the destination_filename already exists, then os.rename canâdepending on our OSâeither overwrite it without warning or throw an exception. So we check beforehand whether that file exists, and if it does, we assume that weâve already handled this image and therefore skip it.
Now, a list of the directory shows the files all renamed according to their titles. We could look in the file manager for this, but weâre in Python, so letâs use Python for it:
>>> import os >>> os.listdir() ['avocado.jpg', 'kiwifruit.jpg', 'lemon.jpg', 'apple.jpg', 'strawberry.jpg', 'rename-images.py', 'orange.jpg']
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12593)
Hello! Python by Anthony Briggs(9928)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9803)
The Mikado Method by Ola Ellnestam Daniel Brolund(9787)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(9382)
Dependency Injection in .NET by Mark Seemann(9348)
Hit Refresh by Satya Nadella(8832)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8310)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7791)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7772)
The Kubernetes Operator Framework Book by Michael Dame(7722)
Grails in Action by Glen Smith Peter Ledbrook(7705)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7567)
Exploring Deepfakes by Bryan Lyon and Matt Tora(7518)
Practical Computer Architecture with Python and ARM by Alan Clements(7441)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(7417)
Robo-Advisor with Python by Aki Ranin(7394)
Building Low Latency Applications with C++ by Sourav Ghosh(7295)
Svelte with Test-Driven Development by Daniel Irvine(7266)
